package com.hevery.web.controller;

import com.hevery.exception.UserNotExistException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Hevery
 * @Description: 处理其他Controller抛出的异常
 * @Date: Created on 2017/10/7
 * @Modified By:
 */
@RestControllerAdvice
public class ControllerExceptionHandler {
    /**当其他Controller抛出UserNotExistException异常时，都会交给这个Controller进行处理*/
    @ExceptionHandler(UserNotExistException.class)
    @ResponseBody
    /**返回Http状态码*/
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    Map<String,Object> handleUserNotExistException(UserNotExistException ex){
        Map<String,Object>result = new HashMap<>();
        result.put("id",ex.getId());
        result.put("message",ex.getMessage());
        return result;
    }
}
